<?php
/**
 * Created by PhpStorm.
 * User: John
 * Date: 17-2-15
 * Time: 上午10:04
 */

namespace Business\TongLian;

use Business\TongLian\AppUtil;

class TongLianPay
{
    //通联配置信息
    private $config = [];
    public function __construct()
    {
        $this->config  = parse_ini_file(HTML_DIR . '/extend/Business/TongLian/tongLianConfig.ini', true);
    }
    /**
     * 通联支付方法
     * @date   2017-02-15
     * @author Lanlc
     *
     * @param  int     $tradeMoney  交易金额
     * @param  int     $orderNum    交易订单号，租户通订单号
     * @param  string  $payType     支付方式：W01微信扫码支付，A01支付宝扫码支付
     * @param  string  $randomStr   生成的随机串码
     * @param  string  $body        订单标题
     * @param  string  $remark      备注信息
     * @param  string  $notify_url  交易结果通知地址
     * @param  string  $limit_pay   支付限制
     *
     * @return
     */
    public function pay($tradeMoney, $orderNum, $payType, $randomStr, $body, $remark)
    {
        $params = array();
        $appUtil = new AppUtil();
        $params["cusid"] = $this->config[ENV]['cusId'];         //商户号
        $params["appid"] = $this->config[ENV]['appId'];         //应用ID
        $params["version"] = $this->config[ENV]['apiVersion'];  //版本号
        $params["trxamt"] = $tradeMoney;
        $params["reqsn"] = $orderNum;//订单号,自行生成
        $params["paytype"] = $payType;
        $params["randomstr"] = $randomStr;//
        $params["body"] = $body;
        $params["remark"] = $remark;
        $params["acct"] = "";
        $params["limit_pay"] = "no_credit";
        $params["notify_url"] = $this->config[ENV]['notifyUrl'];
        $params["sign"] = $appUtil->SignArray($params, $this->config[ENV]['appKey']); //签名

        $paramsStr = $appUtil->ToUrlParams($params);
        $url = $this->config[ENV]['apiUrl'] . "/pay";
        $rsp = $appUtil->request($url, $paramsStr);

        $rspArray = json_decode($rsp, true);
        zft_log('tongLian/pay', print_r($rspArray, true));
        return $rspArray;
    }

    /**
     * 当天交易用撤销
     * @date   2017-02-15
     * @author Lanlc
     *
     * @param  int     $tradeMoney      交易金额
     * @param  int     $orderNum       交易订单号，租户通订单号
     * @param  string  $randomStr      生成的随机串码
     * @param  string  $oldOrderNum    原来的订单号
     *
     * @return
     */
    public function cancel($tradeMoney, $orderNum, $randomStr, $oldOrderNum = '')
    {
        $params = array();
        $appUtil = new AppUtil();
        $params["cusid"]   = $this->config[ENV]['cusId'];
        $params["appid"]   = $this->config[ENV]['appId'];
        $params["version"] = $this->config[ENV]['apiVersion'];

        $params["trxamt"]    = $tradeMoney;
        $params["reqsn"]     = $orderNum;
        $params["oldreqsn"]  = $oldOrderNum;//原来订单号
        $params["randomstr"] = $randomStr;//

        $params["sign"] = $appUtil->SignArray($params, $this->config[ENV]['appKey']);//签名
        $paramsStr = $appUtil->ToUrlParams($params);
        $url = $this->config[ENV]['apiUrl'] . "/cancel";
        $rsp = $appUtil->request($url, $paramsStr);
        echo "请求返回:".$rsp;
        echo "<br/>";
        $rspArray = json_decode($rsp, true);
        if ($this->validSign($rspArray)) {
            echo "验签正确,进行业务处理";
        }
    }

    /**
     * 当天交易请用撤销,非当天交易才用此退货接口
     * @date   2017-02-15
     * @author Lanlc
     *
     * @param  int     $tradeMoney      交易金额
     * @param  int     $orderNum       交易订单号，租户通订单号
     * @param  string  $randomStr   生成的随机串码
     * @param  string  $oldOrderNum    原来的订单号
     *
     * @return
     */
    public function refund()
    {
        $params = array();
        $appUtil = new AppUtil();
        $params["cusid"]     = $this->config[ENV]['cusId'];
        $params["appid"]     = $this->config[ENV]['appId'];
        $params["version"]   = $this->config[ENV]['apiVersion'];
        $params["trxamt"]    = "1";
        $params["reqsn"]     = "1234567889";
        $params["oldreqsn"]  = "123456";//原来订单号
        $params["randomstr"] = "1450432107647";//
        $params["sign"] = $appUtil->SignArray($params, $this->config[ENV]['appKey']);//签名
        $paramsStr = $appUtil->ToUrlParams($params);
        $url = $this->config[ENV]['apiUrl'] . "/refund";
        $rsp = $appUtil->request($url, $paramsStr);
        echo "请求返回:".$rsp;
        echo "<br/>";
        $rspArray = json_decode($rsp, true);
        if ($this->validSign($rspArray)) {
            echo "验签正确,进行业务处理";
        }
    }

    public function query()
    {
        $params = array();
        $appUtil = new AppUtil();
        $params["cusid"]     = $this->config[ENV]['cusId'];
        $params["appid"]     = $this->config[ENV]['appId'];
        $params["version"]   = $this->config[ENV]['apiVersion'];
        $params["reqsn"]     = "123456";
        $params["randomstr"] = "1450432107647";//
        $params["sign"] = $appUtil->SignArray($params, $this->config[ENV]['appKey']);//签名
        $paramsStr = $appUtil->ToUrlParams($params);
        $url = $this->config[ENV]['apiUrl'] . "/query";
        $rsp = $appUtil->request($url, $paramsStr);
        echo "请求返回:".$rsp;
        echo "<br/>";
        $rspArray = json_decode($rsp, true);
        if ($this->validSign($rspArray)) {
            echo "验签正确,进行业务处理";
        }
    }

    //验签
    private function validSign($array)
    {
        $appUtil = new AppUtil();
        if ("SUCCESS" == $array["retcode"]) {
            $signRsp = strtolower($array["sign"]);
            $array["sign"] = "";
            $sign =  strtolower($appUtil->SignArray($array, $this->config[ENV]['appKey']));
            if ($sign==$signRsp) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
